#ifndef QTTHREADGRAPHICS_H
#define QTTHREADGRAPHICS_H

#include <QtGui>

#include "Graphics.h"
#include "Field.h"
#include "GameWidget.h"
#include "RenderThread.h"

  // QImage graphcis in seperate thread!
class QtThreadGraphics : public QObject, public Graphics
{
    Q_OBJECT

    public:
        QtThreadGraphics()
            :render( QImage( Field::WIDTH * RenderThread::WIDTH,
                            Field::HEIGHT * RenderThread::HEIGHT,
                             QImage::Format_RGB32 ) ),
             thread(new RenderThread), widget(new GameWidget),
             halluMode(false), zoomX(20), zoomY(20),
             zoomCenterX(Field::WIDTH / 2), zoomCenterY(Field::HEIGHT / 2)
        {
            connect( this, SIGNAL( paintSignal( int, int, int,
                                                int, bool, Field* ) ),
                     thread, SLOT( needsPainting( int, int, int,
                                                  int, bool, Field* ) ) );

            qRegisterMetaType<QImage>( "QImage" );
            connect( this, SIGNAL( drawAll( GameWidget*, bool ) ),
                     thread, SLOT( needsDrawing(GameWidget*, bool ) ) );
        }

        void paintAll();

        void saveScreen();
        void setHalluMode( bool i ) { halluMode = i; }
        void setZoomToPlayer( bool i ) { thread->setZoomToPlayer( i ); }

          // zooming functions
        void zoomIn(   int i );
        void zoomOut(  int i );
        void zoomInX(  int i );
        void zoomInY(  int i );
        void zoomOutX( int i );
        void zoomOutY( int i );

        void moveZoomCenterUp( int i );
        void moveZoomCenterDown( int i );
        void moveZoomCenterRight( int i );
        void moveZoomCenterLeft( int i );

        GameWidget* playerInput() { return widget; }
        GameWidget* getWidget(){ return widget; }
        bool getHalluMode() { return halluMode; }

        virtual ~QtThreadGraphics() {
            delete widget;
            delete thread;
        }
    public slots:
    signals:
        void paintSignal( int, int, int, int, bool, Field* );
        void drawAll( GameWidget* widget, bool halluMode );
    private:
        static const int MINIMUMSCALEFACTOR = 5;

        QImage render;
        RenderThread* thread;
        GameWidget* widget;
          // if true paints field in reversed colours
        bool halluMode;
          // amount of objects shown around player
        int zoomX, zoomY;
          // center of shown area
        int zoomCenterX, zoomCenterY;
};

#endif // QTTHREADGRAPHICS_H
